package com.xianyun.book.event.handler;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.dto.Response;
import com.xianyun.book.common.core.event.EventHandler;
import com.xianyun.book.common.core.event.EventHandlerI;
import com.xianyun.book.domain.book.Book;
import com.xianyun.book.domain.book.BookStatus;
import com.xianyun.book.domain.gateway.BookGateway;
import com.xianyun.book.dto.domainevent.BookQuerySuccessEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 书籍查询成功事件
 * @author macos-zyj
 */
@CatchAndLog
@Slf4j
@EventHandler
public class BookQuerySuccessHandler implements EventHandlerI<Response, BookQuerySuccessEvent> {

    @Autowired
    private BookGateway bookGateway;

    @Override
    public Response execute(BookQuerySuccessEvent queryEvent) {
        Book queryResult = new Book();
        queryResult.setBookName(queryEvent.getBookName());
        queryResult.setImgUrl(queryResult.getImgUrl());
        queryResult.setBookStatus(BookStatus.getStatusByNumber(queryEvent.getBookStatus()));
        queryResult.setPress(queryResult.getPress());
        queryResult.setPersonNumber(queryEvent.getPersonNumber());
        queryResult.setScore(queryResult.getScore());
        queryResult.setIsbn(queryEvent.getIsbn());
        queryResult.setAuthor(queryResult.getAuthor());
        queryResult.setPrice(queryEvent.getPrice());
        queryResult.setDesc(queryResult.getDesc());
        queryResult.setPublishDate(queryEvent.getPublishDate());
        queryResult.setBookTypes(bookGateway.conversionTypeList(queryEvent.getTypeList()));
        bookGateway.save(queryResult);
        return Response.buildSuccess();
    }
}
